package com.bianmaba.oauth2.authorizationcode.controller;

import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.Map;

@RestController
public class GlobalController {

    /**
     * 此函数可以直接返回principal
     *
     * @param principal
     * @return
     */
    @RequestMapping({"/user", "/me"})
    public Object user(Principal principal) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("name", principal.getName());
        if (principal instanceof OAuth2Authentication) {
            OAuth2Authentication oa = (OAuth2Authentication) principal;
            map.put("authorities", oa.getAuthorities());
            // map.put("userAuthentication", oa.getUserAuthentication());
            map.put("details", oa.getUserAuthentication().getDetails());
            return oa.getUserAuthentication();
        }
        return map;
    }

}
